package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.luo.config.Log;
import com.luo.pojo.Movie;
import com.luo.pojo.RedisCacheEnum;
import com.luo.pojo.Tag;
import com.luo.service.movie.MovieService;
import com.luo.utils.RedisUtil;
import com.luo.utils.getPageInfoUtil;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;


@RestController
public class MovieController {
    @Resource
    private MovieService movieService;
    @Resource
    private RedisUtil redisUtil;
    protected List<Movie> watching;
    protected List<Movie> want;
    protected List<Movie> watched;
    @GetMapping("/movies")
    public Object getAllmovies(HttpServletRequest request){
        HashMap<String,List<Movie>> allMovie = new HashMap<>();
        String movies = (String) redisUtil.get(RedisCacheEnum.Movie.getKey());
        if (movies == null){
            //获取在观看的
            watching = movieService.queryWatching();
            //获取未观看的
            want = movieService.queryWantToWatch();
            //获取已观看的
            watched = movieService.queryWatchedMovies();
            allMovie.put("wantToWatch",want);
            allMovie.put("watched",watched);
            allMovie.put("watching",watching);
//            redisUtil.set(RedisCacheEnum.Movie.getKey(),JSON.toJSONString(allMovie));
            return allMovie;
        }else{
            JSONObject jsonObject = JSON.parseObject(movies);
            return jsonObject;
        }

    }

    @PostMapping("/movies/watched")
    public List<Movie> queryWatched(HttpServletRequest request,@RequestBody HashMap<String,String> page){
        return new getPageInfoUtil<Movie>(page).getPageInfo(page,this.watched);
    }

    @PostMapping("/movies/watching")
    public List<Movie> queryWatching(HttpServletRequest request, @RequestBody HashMap<String,String> page){
       return new getPageInfoUtil<Movie>(page).getPageInfo(page,this.watching);
    }

    @PostMapping("/movies/want")
    public List<Movie> queryWant(HttpServletRequest request,@RequestBody HashMap<String,String> page){
        return new getPageInfoUtil<Movie>(page).getPageInfo(page,this.want);
    }

    @Log(logStr = "新增电影")
    @PostMapping("/movies/add")
    public int insertMovie(HttpServletRequest request,@RequestBody HashMap<String,Object> movie){
        String name = (String) movie.get("name");
        String link = (String)movie.get("link");
        String img = (String)movie.get("img");
        int status = Integer.parseInt((String) movie.get("status"));
        String meta = (String)movie.get("meta");
        String content = (String) movie.get("content");
        try{
            int i = this.movieService.insertMovie(name, link, img, status, meta, content);
            if (i>0){
//                redisUtil.del(RedisCacheEnum.Movie.getKey());
                return i;
            }else{
                return 0;
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @PostMapping("/getMovies")
    public List<Movie> queryAll(@RequestBody HashMap<String,String> page){
        List<Movie> movies = this.movieService.selectAllMovie();
        try{
            return new getPageInfoUtil<Movie>(page).getPageInfo(page,movies);
        }catch (NumberFormatException e){
            return movies;
        }
    }

    @Log(logStr = "删除一则电影")
    @DeleteMapping("/deleteMovie/{mid}")
    public int deleteMovieById(@PathVariable("mid") int mid){
        try{
            int i = this.movieService.deleteMovieById(mid);
            if (i>0){
//                redisUtil.del(RedisCacheEnum.Movie.getKey());
                return i;
            }else{
                return 0;
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    };

    @GetMapping("/getMovies/{name}")
    public List<Movie> queryMovies(@PathVariable("name")String name){
        try{
            return this.movieService.getMoviesByName(name);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @PutMapping("/updateMovie")
    public int updateMovie(@RequestBody Object data){
        String s = JSON.toJSONString(data);
        JSONObject m = JSON.parseObject(s);
        System.out.println(m.get("status"));
        try{
            Movie movie = new Movie((int)m.get("mid"),(String) m.get("name"),(String) m.get("link"),(String) m.get("img"),(int)m.get("status"),(String) m.get("meta"),(String) m.get("content"));
            int i = this.movieService.updateMovie(movie);
            if (i>0){
//                redisUtil.del(RedisCacheEnum.Movie.getKey());
                return i;
            }else{
                return 0;
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}
